package com.lucene.controller;

import com.log.ws.LoggerMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@EnableScheduling
public class WSController {

	@Autowired
	private SimpMessagingTemplate template;

    @RequestMapping(value = "/wlogin",method = RequestMethod.GET)
    public ModelAndView wlogin() {
        return new ModelAndView("wlogin");
    }

    @MessageMapping("/send")
    @SendTo("/topic/send")
    public LoggerMessage send(LoggerMessage message) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        message.setTimestamp(df.format(new Date()));
        return message;
    }

    @Scheduled(fixedRate = 1000)
    @SendTo("/topic/callback")
    public Object callback() throws Exception {
        // 发现消息
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        template.convertAndSend("/topic/callback", df.format(new Date()));
        return "callback";
    }
}
